package com.hwtx.form.assistant;

import io.geekidea.boot.auth.cache.LoginCache;
import io.geekidea.boot.auth.util.LoginUtil;
import io.geekidea.boot.auth.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


@RequestMapping("/assistant")
@RestController
public class AssistantController {

    private final CustomerSupportAssistant agent;

    public AssistantController(CustomerSupportAssistant agent) {
        this.agent = agent;
    }

    @RequestMapping(path = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat(String chatId, String userMessage, String Authorization) {
        if (StringUtils.isEmpty(Authorization)) {
            return Flux.just("认证不通过");
        }
        LoginVo loginVo = LoginUtil.getLoginVo(Authorization);
        if (loginVo == null) {
            return Flux.just("无法获取认证信息");
        }
        LoginCache.set(loginVo);
        Flux<String> flux = agent.chat(chatId, userMessage);
        return flux.concatWithValues("end|");
    }
}
